package Rainwater.Service.impl;

import Rainwater.Service.UserService;
import Rainwater.baen.pojo.User;
import Rainwater.baen.vo.IndexMapPointVo;
import Rainwater.baen.vo.UserDetailedVo;
import Rainwater.baen.vo.UserVo;
import Rainwater.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;

/**
 * @author cfy
 * @date 2024/10/17
 **/
@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;
    @Override
    public ArrayList<UserVo> getUserByPage(Integer pageNumber) {
        //每页条数
        int number = 10;
        //跳过条数
        int page = (pageNumber-1)*number;
        ArrayList<User> usersByPage = userMapper.getUsersByPage(number,page);
        ArrayList<UserVo> userVos = new ArrayList<>();
        for (User user:usersByPage) {
            String identity;
            if (user.getIdentity()==0){
                identity = "管理员";
            }else {
                identity = "普通用户";
            }
            userVos.add(new UserVo(user.getId(),user.getName(),identity));
        }
        return userVos;
    }

    @Override
    public boolean updateIdentity(Integer id) {
        User userById = userMapper.getUserById(id);
        int identity = 1;
        if (userById.getIdentity()==1){
            identity = 0;
        }
        return userMapper.updateIdentity(id, identity);
    }

    @Override
    public boolean deleteUser(Integer id) {
        return userMapper.deleteUserById(id);
    }

    @Override
    public UserDetailedVo getUser(Integer id) {
        User userById = userMapper.getUserById(id);
        System.out.println(userById);
        String identity = "普通用户";
        if (userById.getIdentity()==0){
            identity = "管理员";
        }
        UserDetailedVo detailedVo = new UserDetailedVo(
                userById.getName(),
                userById.getSex(),
                userById.getEmail(),
                identity,
                userById.getBiography());

        return detailedVo;
    }
}
